async function handleSchedule(
  event: ScheduledEvent,
): Promise<Response> {
  const cron = parseCron(event.cron);

  /*
   * Handling specific event to trigger for a CRON trigger
   */
  if (cron === '0 */6 * * *') {
    console.log('cron triggered!');
    return new Response(null, { status: 200 });
  }
  console.log('no event triggered');
  return new Response(null, {
    status: 500,
    statusText: 'Internal server error, cron not detected',
  });
}

function parseCron(cron: string): string {
  cron = cron.replaceAll('/', '');
  if (cron.length > 9) {
    cron = cron.substring(cron.length - 9);
  }
  cron = cron.replaceAll('+', ' ');
  return cron;
}

export const scheduled = async (
  controller: ScheduledEvent,
  env: Env,
  ctx: ExecutionContext
) => {
  ctx.waitUntil(handleSchedule(controller));
};
